import { Injectable } from '@nestjs/common';
import IHelper from '@/services/helper.service';
import IResponse from '@/services/response.service';
import { RedisProvider } from '@/core/providers/redis.provider';
import { concatMap, defer, map } from 'rxjs';

@Injectable()
export class ProxyApiService {
  constructor(
    private readonly redisProvider: RedisProvider
  ) {
  }
  getOneProxy() {
    return defer(() =>
      this.redisProvider.getAllKeys()
    ).pipe(
      map(keys => keys[Math.floor(Math.random() * keys.length)]),
      concatMap(key => this.redisProvider.get(key)),
      map(proxy => proxy ? IResponse.success(proxy) : IResponse.fail())
    )
  }
}